package com.nchu.student_score.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nchu.student_score.mapper.AcademyMapper;
import com.nchu.student_score.model.Academy;
import com.nchu.student_score.service.AcademyService;
import com.nchu.student_score.util.Define;
import com.nchu.student_score.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AcademyServiceImpl implements AcademyService {
    @Autowired
    AcademyMapper academyMapper;

    @Override
    public PageInfo<Academy> getAcademyList(Academy academy, Integer currPage) {
        if(currPage == null) {
            currPage = 1;
        }

        PageHelper.startPage(currPage, Define.PAGE_SIZE);
        PageInfo<Academy> pageInfo;

        if(academy.getAcademyName().trim().length() > 0){
            pageInfo = new PageInfo<>(academyMapper.findAcademy(academy));
        }
        else{
            pageInfo = new PageInfo<>(academyMapper.selectAll());
        }

        return pageInfo;
    }

    @Override
    public ResultVo<Academy> selectOneAcademy(Academy academy) {//按照id查找一个学院
        ResultVo<Academy> resultVo = new ResultVo<>();
        academy = academyMapper.selectByPrimaryKey(academy.getAcademyId());
        resultVo.setObj(academy);
        return resultVo;
    }

    @Override
    public ResultVo<Academy> insertAcademy(Academy insert) {//新增学院
        ResultVo<Academy> resultVo = new ResultVo<>();
        Academy academy = academyMapper.selectByPrimaryKey(insert.getAcademyId());

        if(academy != null){
            resultVo.setStatus(1);
            resultVo.setMessage("该学院编号已存在!");
        }
        else {
            academyMapper.insert(insert);
            resultVo.setStatus(0);
            resultVo.setMessage("新增学院成功!");
        }
        return  resultVo;
    }

    @Override
    public ResultVo<Academy> updateOneAcademy(Academy academy) {//修改学院
        ResultVo<Academy> resultVo = new ResultVo<>();
        academyMapper.updateByPrimaryKey(academy);
        resultVo.setStatus(0);
        resultVo.setMessage("修改成功!");
        return resultVo;
    }

    @Override
    public ResultVo<Academy> selectAcademyCanUse() {
        ResultVo<Academy> resultVo = new ResultVo<>();
        resultVo.setList(academyMapper.selectAcademyCanUse());
        return resultVo;
    }
}
